package com.clt.ess.utils.http;

import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.clt.ess.utils.Base64Utils.encodeBase64File;


public class httpTest {

    public static String city = "南京市";

    public static void main(String[] args) throws Exception {

        //String unitId,String imgBase64,String sealName,String idNum,String city,String unitName,
        //                             String departName,String certName

        List<String> successList = new ArrayList<>();
        List<String> errorList = new ArrayList<>();
        int i = 0;
        int j =0;
        List<SealData> sealDataList = readFile("F:\\江苏省录入数据-1\\");

        String url = "http://localhost:8080/ess_make_system/make/importSeal.html";

        if (sealDataList != null) {
            for(SealData sealData:sealDataList){
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("unitId",sealData.getUnitId());
                dataMap.put("imgBase64",sealData.getImgBase64());
                dataMap.put("sealName",sealData.getSealName());
                dataMap.put("idNum",sealData.getIdNum());
                dataMap.put("city",sealData.getCity());
                dataMap.put("unitName",sealData.getUnitName());
                dataMap.put("departName",sealData.getDepartName());
                dataMap.put("certName",sealData.getCertName());
                String HttpResult = HttpClient.doPost(url,dataMap);
                if("success".equals(HttpResult)){
                    successList.add(sealData.getSealName()+"-----成功\n");
                    i++;
                    if (i/10==2){
                        System.out.println("");
                    }
                    System.out.print(i+"成功");
                }else if ("error1".equals(HttpResult)){
                    successList.add(sealData.getSealName()+"-----已存在手签\n");
                    j++;
                    if (i/10==2){
                        System.out.print("");
                    }
                    System.out.println(j+"失败");
                }else if ("error2".equals(HttpResult)){
                    successList.add(sealData.getSealName()+"-----没找到人员信息\n");
                    j++;
                    if (i/10==2){
                        System.out.print("");
                    }
                    System.out.println(j+"失败");
                }
//                System.out.println(dataMap);
            }
        }
        System.out.println(i+"成功");
        System.out.println(j+"失败");
        System.out.println(successList);
        System.out.println(errorList);

    }

    public static List<SealData> readFile(String path) throws Exception {
        int i = 0;
        List<SealData> sealDataList = new ArrayList<>();
        //循环取值
        File file = new File(path);
        if (file.exists()) {
            //遍历单位文件夹
            File[] fileUnitList = file.listFiles();
            if (null == fileUnitList || fileUnitList.length == 0) {
                System.out.println("总文件夹是空的!");
                return null;
            }
            for (File fileUnit:fileUnitList){
                //遍历出部门文件夹
                File[] fileDepartList = fileUnit.listFiles();
                if (null == fileDepartList || fileDepartList.length == 0) {
                    System.out.println("单位文件夹是空的!");
                    return null;
                }
                String unitString = fileUnit.getName();
                String unitId = unitString.split("-")[0];
                String unitName = unitString.split("-")[1];

                for (File fileDepart:fileDepartList){
                    //遍历出手签文件
                    File[] sealFileList = fileDepart.listFiles();
                    if (null == sealFileList || sealFileList.length == 0) {
                        System.out.println("部门文件夹是空的!");
                        continue;
//                        return null;
                    }
                    for (File sealFile:sealFileList){
                        SealData sealData = new SealData();
                        String data = sealFile.getName();
                        String idNum ="";
                        try {
                            idNum = data.substring(data.length()-22,data.length()-4);
                        }catch (Exception e){
                            System.out.println(data+"---"+unitName);
                            sealFile.deleteOnExit();
                            continue;
                        }
                        String reg = "[^\u4e00-\u9fa5]";
                        String sealName = data.replaceAll(reg, "");
                        String imgBase64 = encodeBase64File(sealFile.getAbsolutePath());

//                        JSONObject js = new JSONObject();
//                        js.put("pageNum","1");
//                        js.put("idCard",idNum);
//                        String sendPost = EssHttpUtil.sendPost("http://10.40.4.190:9812/datacap/service/common/access/get_ygbm_org.do",
//                                js.toJSONString());
//                        JSONObject jsonObject = JSONObject.parseObject(sendPost);
//                        if (jsonObject.get("data").toString().length()>1){
//                            //成功
//                            i++;
////                            System.out.println(i);
//                        }else{
//                            //失败
//                            System.out.println(data);
//                        }
//                        data = data.replaceAll("[\u4e00-\u9fa5]+", "-");
//
//                        String phone = data.split("-")[0];
//                        String idNum = data.split("-")[1];
//
//                        if (phone.length()!=11||idNum.length()!=22){
//                            System.out.println(data);
//                        }

                        sealData.setCity(unitName.substring(0,3));
                        sealData.setUnitName(unitName);
                        sealData.setUnitId(unitId);
                        sealData.setImgBase64(imgBase64);
                        sealData.setDepartName(fileDepart.getName());

                        sealData.setCertName(sealName);

                        sealData.setIdNum(idNum);
                        sealData.setSealName(sealName);

                        sealDataList.add(sealData);
                    }
                }
            }
        }
//        System.out.println(i);
        return sealDataList;
    }


}
